package com.example.demo.msg;

import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.stereotype.Component;

import javax.jms.*;

/**
 * Created by crowndint on 2018/10/1.
 */

/*
   消息订阅者
 */
@Component
public class MsgSubscriber1 {

    @JmsListener(destination = "destination_topic", containerFactory = "getContainerFactory1")
    public void subscribe(String text) {

        System.out.println("MsgSubscriber1 接受到广播消息:"+text);
    }

    /*
        JmsListenerContainerFactory 监听容器工厂:
            负责接收消息以及将消息分发给指定的监听器
     */
    @Bean
    JmsListenerContainerFactory getContainerFactory1(ConnectionFactory connectionFactory) {
        SimpleJmsListenerContainerFactory containerFactory = new SimpleJmsListenerContainerFactory();
        containerFactory.setConnectionFactory(connectionFactory);
        //开启发布订阅模式
        containerFactory.setPubSubDomain(true);
        return containerFactory;
    }
    /*
         <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
             <property name="connectionFactory" ref="connectionFactory" />
             <property name="destination" ref="topicTextDestination" />
             <property name="messageListener" ref="myMessageListener" />
         </bean>
     */




}
